//
//  KAInputView.swift
//  kano
//
//  Created by Sophia on 2024/8/19.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift

class KAInputView: UIView {
    
    private var showLeftView: Bool = false
    private var lastDate: Date?
    private let disposeBag = DisposeBag()
    
    var tfModel: KATFModel!
    var editingBlock: ((KAInputView, String) -> Void)?
    var editDidEndBlock: ((KAInputView, KATFModel) -> Void)?
    
    private lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.colorf2f2f5
        view.layer.masksToBounds = true
        view.layer.cornerRadius = kscale(10)
        return view
    }()
    
    private lazy var leftLabel: UILabel = {
        let label = UILabel()
        label.text = "+234"
        label.textColor = .black
        label.font = UIFont.systemFont(ofSize: kscale(14))
        return label
    }()
    
    private var verticalLine: UIView = {
        let whiteLine = UIView()
        whiteLine.backgroundColor = .color8A8891
        return whiteLine
    }()
    
    lazy var contentTF: UITextField = {
        let contentTF = UITextField()
        contentTF.keyboardType = .numberPad
        contentTF.font = UIFont.boldSystemFont(ofSize: kscale(12))
        contentTF.textColor = UIColor.black
        
        let holderText = "Please enter your phone number"
        let placeholder = NSMutableAttributedString(string: holderText)
        placeholder.addAttributes([
            .font: UIFont.boldSystemFont(ofSize: kscale(16)),
            .foregroundColor: UIColor.color8A8891
        ], range: NSRange(location: 0, length: holderText.count))
        contentTF.attributedPlaceholder = placeholder
        contentTF.keyboardType = .numberPad;
        return contentTF
    }()
    
    private lazy var clearBtn: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "input_clear"), for: .normal)
        button.setImage(UIImage(named: "input_clear"), for: .selected)
        button.rx.tap
            .subscribe(onNext: { [weak self] in
                print("Button was tapped")
                self?.contentTF.text = ""
                self?.clearBtn.isHidden = true
            })
            .disposed(by: disposeBag)
        return button
    }()
    
    init(showLeftView: Bool = false) {
        self.showLeftView = showLeftView
        super.init(frame: .zero)
        setUI()
        bindUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KAInputView {
    
    private func setUI() {
        tfModel = KATFModel()
        backgroundColor = .clear
        addSubview(backView)
        
        if showLeftView {
            backView.addSubview(leftLabel)
            backView.addSubview(verticalLine)
            backView.addSubview(contentTF)
        } else {
            backView.addSubview(contentTF)
        }
        backView.addSubview(clearBtn)
        clearBtn.isHidden = contentTF.text?.isEmpty ?? true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        backView.snp.makeConstraints { make in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        clearBtn.snp.makeConstraints { make in
            make.right.equalTo(backView).offset(kscale(-18))
            make.centerY.equalTo(backView)
            make.size.equalTo(CGSize(width: kscale(24), height: kscale(24)))
        }
        
        if showLeftView {
            leftLabel.snp.makeConstraints { make in
                make.left.equalTo(kscale(20))
                make.centerY.equalTo(backView)
                make.width.equalTo(kscale(56))
            }
            contentTF.snp.makeConstraints { make in
                make.left.equalTo(leftLabel.snp.right).offset(kscale(10))
                make.right.equalTo(kscale(-10))
                make.centerY.equalTo(backView)
            }
            verticalLine.snp.makeConstraints { make in
                make.left.equalTo(leftLabel.snp.right).offset(-kscale(8))
                make.top.equalTo(kscale(16))
                make.bottom.equalTo(kscale(-16))
                make.width.equalTo(kscale(3))
            }
        } else {
            contentTF.snp.makeConstraints { make in
                make.left.equalTo(kscale(10))
                make.right.equalTo(kscale(-10))
                make.centerY.equalTo(backView)
            }
        }
        
       
    }
    
    func callKeyBoard() {
        contentTF.becomeFirstResponder()
    }
    
    func bindUI() {
//        contentTF.rx.text
//            .orEmpty
//            .subscribe(onNext: { text in
//                print("Text changed: \(text)")
//            })
//            .disposed(by: disposeBag)
        
        // editingDidBegin
        contentTF.rx.controlEvent(.editingDidBegin)
            .subscribe(onNext: {
                print("Editing began")
            })
            .disposed(by: disposeBag)
        // editingChanged
        contentTF.rx.controlEvent(.editingChanged)
            .subscribe(onNext: {
                print("Editing Changed")
            })
            .disposed(by: disposeBag)
        // editingDidEnd
        contentTF.rx.controlEvent(.editingDidEnd)
            .subscribe(onNext: {
                print("Editing ended")
            })
            .disposed(by: disposeBag)
        
        contentTF.rx.text
                .orEmpty
                .map { text -> String in
                    return text.count > 11 ? String(text.prefix(11)) : text
                }
                .bind(to: contentTF.rx.text)
                .disposed(by: disposeBag)
    }
    
}
